Veronica Helms

Date: December 3rd, 2016

Program Functions: Ingest ACS Data

ACS Description (Directly from ACS API Website)

  • "The American Community Survey (ACS) is a nationwide survey designed to provide communities a fresh look at how they are changing. The ACS replaced the decennial census long form in 2010 and thereafter by collecting long form type information throughout the decade rather than only once every 10 years. Questionnaires are mailed to a sample of addresses to obtain information about households -- that is, about each person and the housing unit itself. The American Community Survey produces demographic, social, housing and economic estimates in the form of 1-year, 3-year and 5-year estimates based on population thresholds. The strength of the ACS is in estimating population and housing characteristics. It produces estimates for small areas, including census tracts and population subgroups. Although the ACS produces population, demographic and housing unit estimates,it is the Census Bureau's Population Estimates Program that produces and disseminates the official estimates of the population for the nation, states, counties, cities and towns, and estimates of housing units for states and counties. For 2010 and other decennial census years, the Decennial Census provides the official counts of population and housing units."

Specify ACS Variables that will be pulled:

  • population = B01003_001E
  • median househould income = B19013_001E
  • median gross rent = B25064_001E
  • median monthly mortgage cost of those with a mortage = 25088_002E
  • median age of males = B01002_002E
  • median age of females = B01002_003E
  • Below 100 percent of the poverty level = B07012_002E

1. Ingest ACS Data


In [83]:
# Import Dependencies #
import os
import requests

In [84]:
# Create Data Frames # 
censusKey="683a4bd2b0db3ca927d4fe5dd14df9275be4cb7c"
varlist="NAME,B01003_001E,B19013_001E,B25064_001E,B07012_002E,B25088_002E,B01002_002E,B01002_003E"
year = ["2014"]

In [85]:
def api_download(year):

    path_year = os.path.join(os.getcwd(),"ACS", year)
    file_name = path_year + "/" + "ACS" + year + ".txt"
    url="http://api.census.gov/data/"+year+"/acs5?get="+varlist+"&for=county:*&key="+censusKey
    
    if not os.path.exists(path_year):
        os.makedirs(path_year)

    else:
        census_data = requests.get(url)
        f = open(file_name, "w")
        f.write(census_data.text.encode('utf-8'))
        f.close()

def main():
    """
    Main execution
    """
    for year in years:
        api_download(year)

if __name__ == '__main__':
    main()

In [86]:
import pandas as pd
import os 
import csv
import xlrd

filepath = os.path.join(os.getcwd(),"ACS", "2014", "ACS2014.txt")
df= pd.read_csv(filepath)

In [87]:
list(df.columns.values)


Out[87]:
['[["NAME"',
 'B01003_001E',
 'B19013_001E',
 'B25064_001E',
 'B07012_002E',
 'B25088_002E',
 'B01002_002E',
 'B01002_003E',
 'state',
 'county]',
 'Unnamed: 10']